<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;
class Auth extends Base
{
    /**
     * 为数组 / JSON 序列化准备日期。
     *
     * @param  \DateTimeInterface  $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }
    
    // 获取全部数据
    public function getAllList() {
        $data = self::orderBy('created_at','asc')->get()->toArray();
        return $this->treeLevel($data);
    }

    /**
     * 获取顶级权限下拉列表
     */
    public static function getTopAuth($pid=0){
        $data = self::where('pid',0)->get();
        $arr = [];
        if($data){
            foreach($data as $key=>$value){
                $arr[$key]['id'] = $value->id;
                $arr[$key]['name'] = $value->name;
            }
        }
        array_unshift($arr,['id'=>0,'name'=>'==顶级==']);
        return $arr;
    }
    /**
     * 获取权限层级数据（用于菜单列表）
     * @allow_auth 用户拥有的权限
     */
    public function treeData($allow_auth) {
        $query = self::where('is_menu', '1');
        if(is_array($allow_auth)) {
            $query->whereIn('id',array_keys($allow_auth));
        }
        $menuData = $query->get()->toArray();
        return $this->subTree($menuData);
    }
    /**
     * 获取权限层级数据（用于展示所有权限）
     */
    public function allAuthList() {
        $data = self::get()->toArray();
        return $this->subTree($data);
    }


}
